﻿using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class IPHandel  {

    static string IPpath = Application.persistentDataPath + "/IP";
    static string IPFilename = "ip.json";

    public static void SaveIP(string comnane)
    {
        if (!Directory.Exists(IPpath))
        {
            Directory.CreateDirectory(IPpath);
        }
        IPFilename = Path.Combine(IPpath, IPFilename);
        if (!File.Exists(IPFilename))
        {
            FileStream fs = File.Create(IPFilename);
            fs.Close();
        }
        JsonData COMInfor = new JsonData();
        COMInfor["IP"] = comnane;
        string json = COMInfor.ToJson();
        File.WriteAllText(IPFilename, json, Encoding.UTF8);
    }

    public static string ReadIP()
    {
        if (!Directory.Exists(IPpath))
        {
            return "";
        }
        IPFilename = Path.Combine(IPpath, IPFilename);
        if (!File.Exists(IPFilename))
        {
            return "";
        }

        string json = File.ReadAllText(IPFilename, Encoding.UTF8);

        JsonData jsonData = JsonMapper.ToObject(json);

        return (string)jsonData["IP"];
    }
}
